package com.luboyan.CBillbill.controller;

import com.luboyan.CBillbill.annotation.ApiLimited;
import com.luboyan.CBillbill.annotation.DataLimited;
import com.luboyan.CBillbill.advice.TokenVerify;
import com.luboyan.CBillbill.dao.user.UserMoments;
import com.luboyan.CBillbill.service.user.UserMomentsService;
import com.luboyan.CBillbill.util.Result;
import com.luboyan.CBillbill.util.constat.UserConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("users")
public class UserMomentsController {

    @Autowired
    private UserMomentsService userMomentsService;

    @Autowired
    private TokenVerify tokenVerify;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 当前用户新增用户动态
     */
    @PostMapping("/moments")
    @ApiLimited(limitedRoles = {UserConstant.ROLE_CODE_LV0})
    @DataLimited
    public Result<String> addUserMoments(@RequestBody UserMoments userMoments) throws Exception{
        Long userId = tokenVerify.getCurrentUserId();
        userMoments.setUserId(userId);
        userMomentsService.addUserMoments(userMoments);
        return Result.success();
    }

    /**
     * 获取当前用户的订阅用户的动态
     */
    @GetMapping("/subscribed-moments")
    public Result<List<UserMoments>> getUserSubscribedMoments(){
        Long userId = tokenVerify.getCurrentUserId();
        List<UserMoments> result = userMomentsService.getUserSubscribedMoments(userId);
        return new Result<>(result);
    }

}
